import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class Frame1 {

	protected Shell shell;
	private Text InputText;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Frame1 window = new Frame1();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(621, 400);
		shell.setText("Individual task");

		Label lblFirstJavaProject = new Label(shell, SWT.NONE);
		lblFirstJavaProject.setFont(SWTResourceManager.getFont(
				"Tempus Sans ITC", 12, SWT.BOLD));
		lblFirstJavaProject.setBounds(286, 10, 47, 21);
		lblFirstJavaProject.setText("Hello!");

		Label lblRazmochaevaN = new Label(shell, SWT.NONE);
		lblRazmochaevaN.setFont(SWTResourceManager.getFont("Tempus Sans ITC",
				10, SWT.BOLD));
		lblRazmochaevaN.setBounds(455, 331, 142, 21);
		lblRazmochaevaN.setText("Razmochaeva N, 2382");

		InputText = new Text(shell, SWT.BORDER);
		InputText
				.setToolTipText("Enter a String elem and select an action  below");
		InputText.setBounds(106, 37, 484, 21);

		final List list = new List();

		Button btnPopFront = new Button(shell, SWT.NONE);
		btnPopFront.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (!list.isEmpty()) {
					list.popFront();
				} else {
					MessageDialog.openError(shell, "Error!",
							"\t List is empty!");
					return;
				}
			}
		});
		btnPopFront.setToolTipText("remove elem to the top of list");
		btnPopFront.setBounds(250, 71, 100, 25);
		btnPopFront.setText("Pop front");

		Button btnPopEnd = new Button(shell, SWT.NONE);
		btnPopEnd.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (!list.isEmpty()) {
					list.popEnd();
				} else {
					MessageDialog.openError(shell, "Error!",
							"\t List is empty!");
					return;
				}
			}
		});
		btnPopEnd.setToolTipText("remove elem to the end of list");
		btnPopEnd.setBounds(370, 71, 103, 25);
		btnPopEnd.setText("Pop end");

		Button btnPushFront = new Button(shell, SWT.NONE);
		btnPushFront.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String newElem;
				try {
					newElem = InputText.getText();
				} catch (Exception exc) {
					MessageDialog.openError(shell, "Attention!",
							"\t Bad input elem");
					return;
				}
				if (newElem == "") {
					MessageDialog.openError(shell, "Attention!",
							"\t Bad input elem");
					return;
				} else {
					list.pushFront(newElem);
				}
				InputText.setText("");
			}
		});
		btnPushFront.setToolTipText("add elem to the top of list");
		btnPushFront.setBounds(10, 71, 100, 25);
		btnPushFront.setText("Push to front");

		Button btnPushEnd = new Button(shell, SWT.NONE);
		btnPushEnd.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String newElem;
				try {
					newElem = InputText.getText();
				} catch (Exception exc) {
					MessageDialog.openError(shell, "Attention!",
							"\t Bad input elem");
					return;
				}
				if (newElem == "") {
					MessageDialog.openError(shell, "Attention!",
							"\t Bad input elem");
					return;
				} else {
					list.pushEnd(newElem);
				}
				InputText.setText("");
			}
		});
		btnPushEnd.setToolTipText("add elem to the end of list");
		btnPushEnd.setBounds(130, 71, 103, 25);
		btnPushEnd.setText("Push at the end");

		final Label lblInfOfList = new Label(shell, SWT.NONE);
		lblInfOfList.setFont(SWTResourceManager.getFont("Times New Roman", 12,
				SWT.BOLD));
		lblInfOfList.setBounds(10, 232, 580, 73);
		lblInfOfList.setText("::");

		Button btnShowMembers = new Button(shell, SWT.NONE);
		btnShowMembers.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (!list.isEmpty()) {
					String memOfList = list.print();
					lblInfOfList.setText(":: " + memOfList);
				} else {
					MessageDialog.openError(shell, "Error!",
							"\t List is empty!");
					return;
				}
			}
		});
		btnShowMembers.setToolTipText("Show members of list");
		btnShowMembers.setBounds(10, 170, 100, 25);
		btnShowMembers.setText("Members");

		Button btnShowLength = new Button(shell, SWT.NONE);
		btnShowLength.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				lblInfOfList.setText(":: " + list.length());
			}
		});
		btnShowLength.setToolTipText("Show the length of list");
		btnShowLength.setBounds(370, 170, 103, 25);
		btnShowLength.setText("Length");

		Button btnHeadValue = new Button(shell, SWT.NONE);
		btnHeadValue.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (!list.isEmpty()) {
					lblInfOfList.setText(":: " + list.headValue());
				} else {
					MessageDialog.openError(shell, "Error!",
							"\t List is empty!");
					return;
				}
			}
		});
		btnHeadValue.setToolTipText("Show the head of list");
		btnHeadValue.setBounds(130, 170, 100, 25);
		btnHeadValue.setText("Top of list");

		Button btnTailValue = new Button(shell, SWT.NONE);
		btnTailValue.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (!list.isEmpty()) {
					lblInfOfList.setText(":: " + list.tailValue());
				} else {
					MessageDialog.openError(shell, "Error!",
							"\t List is empty!");
					return;
				}
			}
		});
		btnTailValue.setToolTipText("Show the and of list");
		btnTailValue.setBounds(250, 170, 103, 25);
		btnTailValue.setText("End of list");

		Label lblInformation = new Label(shell, SWT.NONE);
		lblInformation.setFont(SWTResourceManager.getFont("Tempus Sans ITC",
				12, SWT.BOLD));
		lblInformation.setBounds(10, 201, 142, 25);
		lblInformation.setText("Information:");

		Label lblShow = new Label(shell, SWT.NONE);
		lblShow.setFont(SWTResourceManager.getFont("Tempus Sans ITC", 12,
				SWT.BOLD));
		lblShow.setBounds(10, 143, 65, 21);
		lblShow.setText("Show:");

		Button btnClearInfoField = new Button(shell, SWT.NONE);
		btnClearInfoField.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				lblInfOfList.setText(":: ");
			}
		});
		btnClearInfoField.setBounds(10, 311, 100, 25);
		btnClearInfoField.setText("clear info field");

		Label lblEnterElem = new Label(shell, SWT.NONE);
		lblEnterElem.setFont(SWTResourceManager.getFont("Tempus Sans ITC", 11,
				SWT.BOLD));
		lblEnterElem.setBounds(10, 38, 90, 15);
		lblEnterElem.setText("Enter elem:");

		Button btnCurElem = new Button(shell, SWT.NONE);
		btnCurElem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (!list.isEmpty()) {
					String curElem = list.curValue();
					lblInfOfList.setText(":: " + curElem);
				} else {
					MessageDialog.openError(shell, "Error!",
							"\t List is empty!");
					return;
				}
			}
		});
		btnCurElem.setBounds(490, 170, 103, 25);
		btnCurElem.setText("Current elem");

		Button btnGoNext = new Button(shell, SWT.NONE);
		btnGoNext.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (!list.isEmpty()) {
					list.goNext();
				} else {
					MessageDialog.openError(shell, "Error!",
							"\t List is empty!");
					return;
				}
			}
		});
		btnGoNext.setBounds(490, 71, 100, 25);
		btnGoNext.setText("Go next elem");

		Button btnPushAfterCurr = new Button(shell, SWT.NONE);
		btnPushAfterCurr.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String newElem;
				try {
					newElem = InputText.getText();
				} catch (Exception exc) {
					MessageDialog.openError(shell, "Attention!",
							"\t Bad input elem");
					return;
				}
				if (newElem == "") {
					MessageDialog.openError(shell, "Attention!",
							"\t Bad input elem");
					return;
				} else {
					list.pushCur(newElem);
				}
				InputText.setText("");
			}
		});
		btnPushAfterCurr.setBounds(199, 112, 100, 25);
		btnPushAfterCurr.setText("Push after curr");

		Button btnPopCurrent = new Button(shell, SWT.NONE);
		btnPopCurrent.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (!list.isEmpty()) {
					list.popCur();
				} else {
					MessageDialog.openError(shell, "Error!",
							"\t List is empty!");
					return;
				}
			}
		});
		btnPopCurrent.setBounds(316, 112, 100, 25);
		btnPopCurrent.setText("Pop current");

	}
}
